package com.example.hrsystem.ui.approval.leave;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hrsystem.R;
import com.example.hrsystem.workflow.ApprovalViewModel;
import com.example.hrsystem.workflow.model.LeaveRequest;

import java.util.List;

/**
 * 请假申请列表Activity
 * 显示用户的请假申请历史记录
 */
public class LeaveRequestListActivity extends AppCompatActivity {
    private ApprovalViewModel viewModel;
    private LeaveRequestAdapter adapter;
    private ProgressBar progressBar;
    private LinearLayout emptyStateLayout;
    private int currentUserId = 2; // 统一使用用户ID 2，与其他功能保持一致

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_request_list);

        // 初始化ViewModel
        viewModel = new ViewModelProvider(this).get(ApprovalViewModel.class);

        // 初始化UI组件
        initUI();

        // 设置点击事件监听
        setupListeners();

        // 加载请假申请数据
        loadLeaveRequests();
    }

    /**
     * 初始化UI组件
     */
    private void initUI() {
        progressBar = findViewById(R.id.progress_bar);
        emptyStateLayout = findViewById(R.id.empty_state_layout);
        RecyclerView recyclerView = findViewById(R.id.leave_request_recycler_view);
        Button newRequestButton = findViewById(R.id.new_request_button);

        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        
        adapter = new LeaveRequestAdapter(leaveRequest -> {
            // 点击请假申请项，跳转到详情页面
            Intent intent = new Intent(LeaveRequestListActivity.this, LeaveRequestDetailActivity.class);
            intent.putExtra("leave_request_id", leaveRequest.getId());
            startActivity(intent);
        });
        
        recyclerView.setAdapter(adapter);
    }

    /**
     * 设置事件监听器
     */
    private void setupListeners() {
        // 新建请假申请按钮点击事件
        findViewById(R.id.new_request_button).setOnClickListener(v -> {
            startActivity(new Intent(LeaveRequestListActivity.this, LeaveRequestFormActivity.class));
        });

        // 监听加载状态
        viewModel.getIsLoading().observe(this, isLoading -> {
            progressBar.setVisibility(isLoading ? View.VISIBLE : View.GONE);
        });

        // 监听错误信息
        viewModel.getErrorMessage().observe(this, errorMessage -> {
            if (errorMessage != null && !errorMessage.isEmpty()) {
                Toast.makeText(LeaveRequestListActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 加载请假申请数据
     */
    private void loadLeaveRequests() {
        List<LeaveRequest> leaveRequests = viewModel.getLeaveRequestsByApplicant(currentUserId);
        updateUI(leaveRequests);
    }

    /**
     * 更新UI显示
     */
    private void updateUI(List<LeaveRequest> leaveRequests) {
        RecyclerView recyclerView = findViewById(R.id.leave_request_recycler_view);
        if (leaveRequests != null && !leaveRequests.isEmpty()) {
            adapter.setLeaveRequests(leaveRequests);
            recyclerView.setVisibility(View.VISIBLE);
            emptyStateLayout.setVisibility(View.GONE);
        } else {
            recyclerView.setVisibility(View.GONE);
            emptyStateLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 重新加载数据，确保数据是最新的
        loadLeaveRequests();
    }
}